package com.huobao.yunying.base;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.widget.ImageView;

import com.easemob.chat.EMChat;
import com.huobao.yunying.huanxin.ui.DemoHelper;
import com.huobao.zhangying.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import org.xutils.DbManager;
import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class HuobaoApplication extends MultiDexApplication {
	// 线程池中线程数量
	private static final int THREADSIZE = 8;

	// 线程池
	private static ExecutorService m_ExecutorService;
	public static ImageOptions imageOptions;
	public static DisplayImageOptions options; // DisplayImageOptions是用于设置图片显示的类
	public static ImageLoader imageLoader;

	public DbManager.DaoConfig daoConfig;
	@Override
	public void onCreate() {
		super.onCreate();

		int pid = android.os.Process.myPid();
		String processAppName = getAppName(pid);
		if (processAppName != null && processAppName.equalsIgnoreCase("com.huobao.zhangying")) {
			EMChat.getInstance().setAppkey("596513643#zhangying");
			DemoHelper.getInstance().init(getApplicationContext());
		}
		CrashHandler catchHandler = CrashHandler.getInstance();
		catchHandler.init(getApplicationContext(), this);
		x.Ext.init(this);
		x.Ext.setDebug(false);
		

		imageOptions = new ImageOptions.Builder().setSize(DensityUtil.dip2px(320), DensityUtil.dip2px(320))// 图片大小
				// .setRadius(DensityUtil.dip2px(5))//ImageView圆角半径
				// .setCrop(true)// 如果ImageView的大小不是定义为wrap_content, 不要crop.
				.setImageScaleType(ImageView.ScaleType.FIT_XY).setLoadingDrawableId(R.mipmap.imageloading)// 加载中默认显示图片

				// .setFailureDrawableId(R.mipmap.ic_launcher)//加载失败后默认显示图片
				.build();

		options = new DisplayImageOptions.Builder().showStubImage(R.mipmap.imageloading) // 设置图片下载期间显示的图片
				.showImageForEmptyUri(R.mipmap.imageloading) // 设置图片Uri为空或是错误的时候显示的图片
				.showImageOnFail(R.mipmap.imageloading) // 设置图片加载或解码过程中发生错误显示的图片
				.cacheInMemory(true) // 设置下载的图片是否缓存在内存中
				.cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中
				.build(); // 创建配置过得DisplayImageOption对象

		imageLoader = ImageLoader.getInstance();
		ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(getApplicationContext());
		imageLoader.init(configuration);

		daoConfig = new DbManager.DaoConfig()
				.setDbName("huobao.db")
				// 不设置dbDir时, 默认存储在app的私有目录.
				.setDbVersion(1)
				.setDbOpenListener(new DbManager.DbOpenListener() {
					@Override
					public void onDbOpened(DbManager db) {
						// 开启WAL, 对写入加速提升巨大
						db.getDatabase().enableWriteAheadLogging();
					}
				})
				.setDbUpgradeListener(new DbManager.DbUpgradeListener() {
					@Override
					public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
						// TODO: ...
						// db.addColumn(...);
						// db.dropTable(...);
						// ...
						// or
						// db.dropDb();
					}
				});

	}

	@Override
	protected void attachBaseContext(Context base) {
		super.attachBaseContext(base);
		MultiDex.install(this);
	}

	public String getAppName(int pID) {
		String processName = null;
		ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
		List l = am.getRunningAppProcesses();
		Iterator i = l.iterator();
		PackageManager pm = this.getPackageManager();
		while (i.hasNext()) {
			ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
			try {
				if (info.pid == pID) {
					CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
					// Log.d("Process", "Id: "+ info.pid +" ProcessName: "+
					// info.processName +"  Label: "+c.toString());
					// processName = c.toString();
					processName = info.processName;
					return processName;
				}
			} catch (Exception e) {
				// Log.d("Process", "Error>> :"+ e.toString());
			}
		}
		return processName;
	}
	/**
	 * function：获取创建的线程池 ModifyDatetime: 2015-5-11 上午09:37:37
	 * 
	 * @return
	 */
	public static ExecutorService getExecutorService() {
		if (m_ExecutorService == null)
			createThreadPool();
		return m_ExecutorService;
	}

	// 创建线程池
	private static final void createThreadPool() {
		m_ExecutorService = Executors.newFixedThreadPool(THREADSIZE);
	}
}
